package com.tuanzi.loan.business.repository.statistics.impl;

import com.tuanzi.loan.business.repository.statistics.BigClueStatChannelRepository;
import com.tuanzi.loan.core.repository.JOOQBaseRepositoryPlus;
import com.tuanzi.loan.vo.request.statistics.BigClueStatChannelsearchRequest;
import com.tuanzi.loan.vo.response.statistics.BigClueStatChannelsearchResponse;
import org.jooq.impl.DSL;
import org.jooq.impl.TableImpl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Objects;

import static com.tuanzi.loan.generated.jooq.stat.tables.StatBigClueChannel.STAT_BIG_CLUE_CHANNEL;
@SuppressWarnings({ "rawtypes" })
@Repository
public class BigClueStatChannelRepositoryImpl  extends JOOQBaseRepositoryPlus implements BigClueStatChannelRepository{
    @Override
    protected TableImpl getTable() {
        return STAT_BIG_CLUE_CHANNEL;
    }
    @Override
    public Page<BigClueStatChannelsearchResponse> list(BigClueStatChannelsearchRequest searchRequest, Pageable pageable) {
        if ("-1000".equals(searchRequest.getCfrom_channel())){
            fields(
                    STAT_BIG_CLUE_CHANNEL.TIME,
                    DSL.sum(STAT_BIG_CLUE_CHANNEL.BASEUV).as("baseuv"),
                    DSL.sum(STAT_BIG_CLUE_CHANNEL.BASECOMMIT).as("basecommit"),
                    DSL.sum(STAT_BIG_CLUE_CHANNEL.PRELIMINARYCOMMIT).as("Preliminarycommit"),
                    DSL.sum(STAT_BIG_CLUE_CHANNEL.SUPPLEMENTUV).as("supplementUV"),
                    DSL.sum(STAT_BIG_CLUE_CHANNEL.SUPPLEMENTDELIVER).as("supplementdeliver"),
                    DSL.sum(STAT_BIG_CLUE_CHANNEL.SUCCESSRESULTUV).as("successresultuv"),
                    DSL.sum(STAT_BIG_CLUE_CHANNEL.SUCCESSPUBLIC).as("successpublic"),
                    DSL.sum(STAT_BIG_CLUE_CHANNEL.SUCCESSWECHAT).as("successwechat"),
                    DSL.sum(STAT_BIG_CLUE_CHANNEL.HIGNUSER).as("hignuser")
            );
        }else{
            fields(STAT_BIG_CLUE_CHANNEL.TIME,
                    STAT_BIG_CLUE_CHANNEL.BASEUV,
                    STAT_BIG_CLUE_CHANNEL.BASECOMMIT,
                    STAT_BIG_CLUE_CHANNEL.PRELIMINARYCOMMIT,
                    STAT_BIG_CLUE_CHANNEL.SUPPLEMENTUV,
                    STAT_BIG_CLUE_CHANNEL.SUPPLEMENTDELIVER,
                    STAT_BIG_CLUE_CHANNEL.SUCCESSRESULTUV,
                    STAT_BIG_CLUE_CHANNEL.SUCCESSPUBLIC,
                    STAT_BIG_CLUE_CHANNEL.SUCCESSWECHAT,
                    STAT_BIG_CLUE_CHANNEL.HIGNUSER,
                    STAT_BIG_CLUE_CHANNEL.CFROM_CHANNEL
            );
        }

        if (!"-1000".equals(searchRequest.getCfrom_channel())){
            eq(STAT_BIG_CLUE_CHANNEL.CFROM_CHANNEL,searchRequest.getCfrom_channel());
        }


        if (Objects.nonNull(searchRequest.getStartdate())){
            ge(STAT_BIG_CLUE_CHANNEL.TIME,searchRequest.getStartdate());
        }
        if (Objects.nonNull(searchRequest.getEnddate())){
            le(STAT_BIG_CLUE_CHANNEL.TIME,searchRequest.getEnddate());
        }

        if ("-1000".equals(searchRequest.getCfrom_channel())){
            groupBy(STAT_BIG_CLUE_CHANNEL.TIME);
        }
        orderBy(STAT_BIG_CLUE_CHANNEL.TIME, Sort.Direction.DESC);
        return search(pageable,record -> {
            BigClueStatChannelsearchResponse response = new BigClueStatChannelsearchResponse();
            if ("-1000".equals(searchRequest.getCfrom_channel())){
                response.setCfrom_channel("全部");
            }else{
                response.setCfrom_channel(record.get(STAT_BIG_CLUE_CHANNEL.CFROM_CHANNEL));
            }
            response.setTime(record.get(STAT_BIG_CLUE_CHANNEL.TIME));
            response.setBaseuv(dealNullPoint(record.get(STAT_BIG_CLUE_CHANNEL.BASEUV,Long.class)));
            response.setBasecommit(dealNullPoint(record.get(STAT_BIG_CLUE_CHANNEL.BASECOMMIT,Long.class)));
            response.setPreliminarycommit(dealNullPoint(record.get(STAT_BIG_CLUE_CHANNEL.PRELIMINARYCOMMIT,Long.class)));
            response.setSupplementUV(dealNullPoint(record.get(STAT_BIG_CLUE_CHANNEL.SUPPLEMENTUV,Long.class)));
            response.setSupplementdeliver(dealNullPoint(record.get(STAT_BIG_CLUE_CHANNEL.SUPPLEMENTDELIVER,Long.class)));
            response.setSuccessresultuv(dealNullPoint(record.get(STAT_BIG_CLUE_CHANNEL.SUCCESSRESULTUV,Long.class)));
            response.setSuccesspublic(dealNullPoint(record.get(STAT_BIG_CLUE_CHANNEL.SUCCESSPUBLIC,Long.class)));
            response.setSuccesswechat(dealNullPoint(record.get(STAT_BIG_CLUE_CHANNEL.SUCCESSWECHAT,Long.class)));
            response.setHignuser(dealNullPoint(record.get(STAT_BIG_CLUE_CHANNEL.HIGNUSER,Long.class)));
            return response;
        });
    }

    @Override
    public List<String> returnCfrom() {
        List<String> resultList =
                dslContext.selectDistinct(STAT_BIG_CLUE_CHANNEL.CFROM_CHANNEL).from(STAT_BIG_CLUE_CHANNEL)
                        .fetch(STAT_BIG_CLUE_CHANNEL.CFROM_CHANNEL);
        return resultList;
    }


    private Long dealNullPoint(Long object){
        if (object!=null){
            return object.longValue();
        }
        return 0l;
    }
}
